using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SilverPart;

namespace SilverPart
{
    public class SilverPartEditorPart: EditorPart
    {
        #region Constants

        private const string TITLE_VERSION= "Silverlight plugin version";
        private const string EDITOR_TITLE = "Silverlight Settings";
        private const string SOURCE_TITLE_XAML = "Source File (.XAML)";
        private const string SOURCE_TITLE_XAP = "Source File (.XAP)";
        private const string JAVASCRIPT_TITLE = "Javascript code-behind files";
        private const string JAVASCRIPT_MSG = "Use ; to separate multiple files";
        private const string WIDTH_TITLE = "Control Width";
        private const string HEIGHT_TITLE = "Control Height";
        private const string BACKGROUND_TITLE = "Control Background Color";
        private const string INITPARAMS_TITLE = "Initialization Parameters";
        private const string HTMLACCESS_TITLE = "Enable HTML Access ?";
        private const string WINDOWLESS_TITLE = "Use Windowless Control ?";
        private const string INSTALL_TITLE = "Show Install Banner ?";
        private const string INSTALL_IMAGE_TITLE = "Silverlight Install Image";
        private const string INSTALL_IMAGE_URL = "Silverlight Download URL";
        private const string CURRENTUSER_PARAM = "Current User InitParam";
        private const string CURRENTSITE_PARAM = "Current Site InitParam";
        
        private const string CSS_HEAD = "<div style=\"width:100%;padding:0px 0px 5px 0px;color:#595959;\">";
        private const string CSS_BODY = "<div style=\"width:100%;padding:0px 0px 0px 0px;\">";
        private const string CSS_LINE = "<div style=\"width:100%\" class=\"UserDottedLine\"></div>";

        private const string SL_VERSION10 = "1.0";
        private const string SL_VERSION20 = "2.0";

        #endregion

        #region Private Fields

        //Control to select Silverlight version
        private DropDownList versionSelector;

        //Control to specify Silverlight source
        private TextBox silverlightSourceFile;

        //Control to specify Javascript code-behind file
        private TextBox javascriptFile;

        //Control to specify the SL control's width
        private TextBox controlWidth;

        //Control to specify the SL control's height
        private TextBox controlHeight;

        //Control to specify the SL background color
        private TextBox controlBackgroundColor;

        //Control to specify the SL InitParams
        private TextBox controlInitParams;

        //Control to check if SL Control can access the page's HTML
        private CheckBox controlHTMLAccessEnabled;

        //Control to check if Install plugin image is shown if plugin is not installed
        private CheckBox showInstallBanner;

        //Control to specify SL Install Image
        private TextBox controlInstallImage;

        //Control to specify SL Install URL
        private TextBox controlInstallURL;

        //Control to check if SL Control can access the page's HTML
        private CheckBox controlHTMLAccess;

        //Control to check if SL Control is Windowless
        private CheckBox controlWindowless;

        //Control to specify if Current User will be passed as an initialization parameter
        private CheckBox currentUserParam;

        //Control to specify if Current Site will be passed as an initialization parameter
        private CheckBox currentSiteParam;
        
        //Title controls
        private Label sourceTitle = new Label();
        private Label versionTitle = new Label();
        private Label javascriptTitle = new Label();
        private Label javascriptMessage = new Label();
        private Label widthTitle = new Label();
        private Label heightTitle = new Label();
        private Label backgroundTitle = new Label();
        private Label initParamsTitle = new Label();
        private Label htmlAccessTitle = new Label();
        private Label windowlessTitle = new Label();
        private Label showInstallImageTitle = new Label();
        private Label installImageTitle = new Label();
        private Label installURLTitle = new Label();
        private Label currentUserTitle = new Label();
        private Label currentSiteTitle = new Label();
    
        #endregion

        #region Constructors

        //Default Constructor
        public SilverPartEditorPart()
        {
            this.Title = EDITOR_TITLE;
        } 

        #endregion
        
        #region EditorPart Override Methods

        public override void SyncChanges()
        {
            this.EnsureChildControls();

            SilverPartWebPart silverPart = (SilverPartWebPart)this.WebPartToEdit;

            if (!string.IsNullOrEmpty(silverPart.SilverlightControlJavaScriptFiles))
            {
                this.javascriptFile.Text = silverPart.SilverlightControlJavaScriptFiles;
            }

            if (!string.IsNullOrEmpty(silverPart.SilverlightControlSource))
            {
               this.silverlightSourceFile.Text = silverPart.SilverlightControlSource;
            }

            if (!string.IsNullOrEmpty(silverPart.SilverlightControlVersion))
            {
                this.versionSelector.SelectedValue = silverPart.SilverlightControlVersion;
                VersionSelectedConfigurator(silverPart.SilverlightControlVersion);
            }

            if (!string.IsNullOrEmpty(silverPart.SilverlightControlWidth))
            {
                this.controlWidth.Text = silverPart.SilverlightControlWidth;
            }

            if (!string.IsNullOrEmpty(silverPart.SilverlightControlHeight))
            {
                this.controlHeight.Text = silverPart.SilverlightControlHeight;
            }

            if (!string.IsNullOrEmpty(silverPart.SilverlightControlBackground))
            {
                this.controlBackgroundColor.Text = silverPart.SilverlightControlBackground;
            }

            if (!string.IsNullOrEmpty(silverPart.SilverlightControlInitParams))
            {
                this.controlInitParams.Text = silverPart.SilverlightControlInitParams;
            }

            if (silverPart.SilverlightControlHtmlAccess)
            {
                this.controlHTMLAccessEnabled.Checked = true;
            }
            else
            {
                this.controlHTMLAccessEnabled.Checked = false;
            }

            if (silverPart.SilverlightControlWindowless)
            {
                this.controlWindowless.Checked = true;
            }
            else
            {
                this.controlWindowless.Checked = false;
            }

            if (silverPart.ShowInstallBanner)
            {
                this.showInstallBanner.Checked = true;
                ShowInstallBannerMethod();
            }
            else
            {
                this.showInstallBanner.Checked = false;
                ShowInstallBannerMethod();
            }

            if (!string.IsNullOrEmpty(silverPart.InstallBannerImage))
            {
                this.controlInstallImage.Text = silverPart.InstallBannerImage;
            }

            if (!string.IsNullOrEmpty(silverPart.InstallBannerURL))
            {
                this.controlInstallURL.Text = silverPart.InstallBannerURL;
            }

            if (silverPart.SilverlightControlCurrentUser)
            {
                this.currentUserParam.Checked = true;
            }
            else
            {
                this.currentUserParam.Checked = false;
            }

            if (silverPart.SilverlightControlCurrentSite)
            {
                this.currentSiteParam.Checked = true;
            }
            else
            {
                this.currentSiteParam.Checked = false;
            }

        }

        public override bool ApplyChanges()
        {
            this.EnsureChildControls();
            
            SilverPartWebPart silverPart = (SilverPartWebPart)this.WebPartToEdit;
            
            if (this.versionSelector.SelectedValue.Equals(SL_VERSION10))
            {
                silverPart.SilverlightControlJavaScriptFiles = this.javascriptFile.Text;
                silverPart.SilverlightControlVersion = SL_VERSION10;
            }
            else
            {
                silverPart.SilverlightControlJavaScriptFiles = String.Empty;
                silverPart.SilverlightControlVersion = SL_VERSION20;
            }

            silverPart.SilverlightControlSource = this.silverlightSourceFile.Text;
            silverPart.SilverlightControlWidth = this.controlWidth.Text;
            silverPart.SilverlightControlHeight = this.controlHeight.Text;
            silverPart.SilverlightControlBackground = this.controlBackgroundColor.Text;
            silverPart.SilverlightControlInitParams = this.controlInitParams.Text;
            silverPart.SilverlightControlHtmlAccess = this.controlHTMLAccessEnabled.Checked;
            silverPart.SilverlightControlWindowless = this.controlWindowless.Checked;
            silverPart.ShowInstallBanner = this.showInstallBanner.Checked;
            silverPart.InstallBannerImage = this.controlInstallImage.Text;
            silverPart.InstallBannerURL = this.controlInstallURL.Text;
            silverPart.SilverlightControlCurrentUser = this.currentUserParam.Checked;
            silverPart.SilverlightControlCurrentSite = this.currentSiteParam.Checked;

            return true;
        }

        protected override void CreateChildControls()
        {
           
            //Version Title
            this.Controls.Add(new LiteralControl("<br/>"));
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.versionTitle.Text = TITLE_VERSION;
            this.Controls.Add(versionTitle);
            this.Controls.Add(new LiteralControl("</div>"));
            
            //Version Selector
            this.versionSelector = new DropDownList();
            this.versionSelector.Items.Add(SL_VERSION10);
            this.versionSelector.Items.Add(SL_VERSION20);
            this.versionSelector.AutoPostBack = true;
            this.versionSelector.SelectedValue = SL_VERSION20;
            this.versionSelector.SelectedIndexChanged += new EventHandler(versionSelector_SelectedIndexChanged);
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.Controls.Add(this.versionSelector);
            this.Controls.Add(new LiteralControl("</div>"));
            
            this.Controls.Add(new LiteralControl(CSS_LINE));
            
           //Javascript Title
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.javascriptTitle.Text = JAVASCRIPT_TITLE;
            this.Controls.Add(javascriptTitle);
            this.Controls.Add(new LiteralControl("</div>"));

            //Javascript Message
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.javascriptMessage.Text = JAVASCRIPT_MSG;
            this.Controls.Add(javascriptMessage);
            this.Controls.Add(new LiteralControl("</div>"));

            //Javascript File
            this.javascriptFile = new TextBox();
            this.javascriptFile.TextMode = TextBoxMode.MultiLine;
            this.javascriptFile.Rows = 3;
            this.Controls.Add(javascriptFile);
          
            //Show and Hide Javascript based on version selected
            VersionSelectedConfigurator(this.versionSelector.SelectedValue);

            //Source file Title
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.sourceTitle.Text = SOURCE_TITLE_XAML;
            this.Controls.Add(this.sourceTitle);
            this.Controls.Add(new LiteralControl("</div>"));

            //Source file
            this.silverlightSourceFile = new TextBox();
            this.Controls.Add(silverlightSourceFile);
            
            //Width Title
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.widthTitle.Text = WIDTH_TITLE;
            this.Controls.Add(this.widthTitle);
            this.Controls.Add(new LiteralControl("</div>"));

            //Control's Width
            this.controlWidth = new TextBox();
            this.Controls.Add(this.controlWidth);

            //Height Title
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.heightTitle.Text = HEIGHT_TITLE;
            this.Controls.Add(this.heightTitle);
            this.Controls.Add(new LiteralControl("</div>"));

            //Control's Height
            this.controlHeight = new TextBox();
            this.Controls.Add(this.controlHeight);

            //Background Color Title
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.backgroundTitle.Text = BACKGROUND_TITLE;
            this.Controls.Add(this.backgroundTitle);
            this.Controls.Add(new LiteralControl("</div>"));

            //Control's Background Color
            this.controlBackgroundColor = new TextBox();
            this.Controls.Add(this.controlBackgroundColor);

            //InitParams Title
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.initParamsTitle.Text = INITPARAMS_TITLE;
            this.Controls.Add(this.initParamsTitle);
            this.Controls.Add(new LiteralControl("</div>"));

            //Control's InitParams
            this.controlInitParams = new TextBox();
            this.Controls.Add(this.controlInitParams);

            //Current User InitParam
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.currentUserParam = new CheckBox();
            this.currentUserTitle.Text = CURRENTUSER_PARAM;
            this.currentUserParam = new CheckBox();
            this.Controls.Add(this.currentUserParam);
            this.Controls.Add(this.currentUserTitle);
            this.Controls.Add(new LiteralControl("</div>"));

            //Current Site InitParam
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.currentSiteParam = new CheckBox();
            this.currentSiteTitle.Text = CURRENTSITE_PARAM;
            this.currentSiteParam = new CheckBox(); 
            this.Controls.Add(this.currentSiteParam);
            this.Controls.Add(this.currentSiteTitle);
            this.Controls.Add(new LiteralControl("</div>"));
            
            //HTML Access Title
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.htmlAccessTitle.Text = HTMLACCESS_TITLE;
            this.controlHTMLAccessEnabled = new CheckBox();
            this.Controls.Add(this.controlHTMLAccessEnabled);
            this.Controls.Add(this.htmlAccessTitle);
            this.Controls.Add(new LiteralControl("</div>"));

            //HTML Access Title
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.windowlessTitle.Text = WINDOWLESS_TITLE;
            this.controlWindowless = new CheckBox();
            this.Controls.Add(this.controlWindowless);
            this.Controls.Add(this.windowlessTitle);
            this.Controls.Add(new LiteralControl("</div>"));

            //Show Install Banner
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.showInstallImageTitle.Text = INSTALL_TITLE;
            this.showInstallBanner = new CheckBox();
            this.showInstallBanner.AutoPostBack = true;
            this.showInstallBanner.CheckedChanged += new EventHandler(showInstallBanner_CheckedChanged);
            this.Controls.Add(this.showInstallBanner);
            this.Controls.Add(this.showInstallImageTitle);
            this.Controls.Add(new LiteralControl("</div>"));

            //Install Image Title
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.installImageTitle.Text = INSTALL_IMAGE_TITLE;
            this.Controls.Add(this.installImageTitle);
            this.Controls.Add(new LiteralControl("</div>"));
            
            //Install Banner Image
            this.controlInstallImage = new TextBox();
            this.Controls.Add(this.controlInstallImage);

            //Install Image URL
            this.Controls.Add(new LiteralControl(CSS_HEAD));
            this.installURLTitle.Text = INSTALL_IMAGE_URL;
            this.Controls.Add(this.installURLTitle);
            this.Controls.Add(new LiteralControl("</div>"));
            
            //Install Banner URL
            this.controlInstallURL = new TextBox();
            this.Controls.Add(this.controlInstallURL);

            ShowInstallBannerMethod();
            
        }

        #endregion

        #region EditorPart Private Methods
        
        //Configure properties according to version selected
        private void VersionSelectedConfigurator(string version)
        {
            if (version == SL_VERSION20)
            {
                this.sourceTitle.Text = SOURCE_TITLE_XAP;
                this.javascriptTitle.Visible = false;
                this.javascriptFile.Visible = false;
                this.javascriptMessage.Visible = false;
                this.javascriptFile.Text = string.Empty;
            }
            else
            {
                this.sourceTitle.Text = SOURCE_TITLE_XAML;
                this.javascriptTitle.Visible = true;
                this.javascriptMessage.Visible = true;
                this.javascriptFile.Visible = true;
            }
        }

        //Event Handler when Show Install Banner is selected
        private void showInstallBanner_CheckedChanged(object sender, EventArgs e)
        {
            ShowInstallBannerMethod();
        }

        private void ShowInstallBannerMethod()
        {
            if (!this.showInstallBanner.Checked)
            {
                this.controlInstallURL.Visible = false;
                this.installURLTitle.Visible = false;
                this.controlInstallImage.Visible = false;
                this.installImageTitle.Visible = false;
                this.controlInstallImage.Text = String.Empty;
                this.controlInstallURL.Text = String.Empty;
            }
            else
            {
                this.controlInstallURL.Visible = true;
                this.installURLTitle.Visible = true;
                this.controlInstallImage.Visible = true;
                this.installImageTitle.Visible = true;
            }
        }

        //Event handler for Version Selector change event
        private void versionSelector_SelectedIndexChanged(object sender, EventArgs e)
        {
            VersionSelectedConfigurator(this.versionSelector.SelectedValue);
        } 

        #endregion       
     }
}